package com.mallcai.bigdata.ladon.controller.peekdata;

import com.cake.bigdata.starter.exception.category.BusinessException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mallcai.bigdata.ladon.controller.peekdata.form.TagForm;
import com.mallcai.bigdata.ladon.dpl.entity.peekdata.Tag;
import com.mallcai.bigdata.ladon.service.peekdata.ITagService;
import com.mallcai.bigdata.ladon.vo.MyPageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author feixy
 * @version 1.0
 * @date 2019-06-25 18:23
 */
@RequestMapping("/peekdata/tag")
@RestController
public class TagManageController {


    @Autowired
    private ITagService iTagService;

    @RequestMapping("/add")
    public String add(@RequestBody @Validated(value = TagForm.AddCheck.class) TagForm form) {
        Tag tag = form.toEntity();

        int status = iTagService.add(tag);
        if (status <= 0) {
            throw new BusinessException("添加标签失败");
        }
        return "添加标签成功";
    }

    @RequestMapping("/getByPage")
    public MyPageInfo getByPage(@RequestBody @Validated TagForm form) {
        PageHelper.startPage(form.getCurrentPage(), form.getPageSize());
        List<Tag> models = iTagService.getByParams(form.getParams());
        PageInfo<Tag> pageInfo = new PageInfo<>(models);
        return new MyPageInfo<>(pageInfo);
    }

    @RequestMapping("/listAll")
    public List<Tag> listAll() {
        return iTagService.listAll();
    }

    @RequestMapping("/del")
    public String delTag(@RequestBody @Validated(value = TagForm.IdCheck.class) TagForm form) {
        Tag tag = form.toEntity();
        int status = iTagService.delete(tag);
        if (status <= 0) {
            throw new BusinessException("删除标签失败");
        }
        return "删除标签成功";
    }


}
